package MCQReaderInterface;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JLabel;

import MCQReaderModel.GroupNode;
import MCQReaderModel.PageNode;

public class MCQReaderLayer extends JLabel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int xClick;
	private int yClick;
	private Rectangle selection;

	public Rectangle getSelection() {
		return selection;
	}

	public MCQReaderLayer() {

		selection = new Rectangle();

		addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent e) {
			}

			public void mousePressed(MouseEvent e) {

				xClick = e.getX();
				yClick = e.getY();
				if (aGroupIsScalled()) {
					PageNode pageNode = MainInterface.thisIsIt.getLeftFrame()
							.getTree().currentNodePage();
					MCQReaderButtons buttons = MainInterface.thisIsIt
							.getLeftFrame().getButtons();
					GroupNode node;
					if (buttons.getTypeSelected() == 0)/* Radios */
						node = (GroupNode) pageNode.getRadioNode().getChildAt(
								buttons.getGroupeSelected() - 1);
					else
						/* Cases */
						node = (GroupNode) pageNode.getCaseNode().getChildAt(
								buttons.getGroupeSelected() - 1);
					int height = node.getHeight();
					int width = node.getWidth();
					selection.setBounds(e.getX() - width / 2, e.getY() - height
							/ 2, width, height);
					MainInterface.thisIsIt.getLeftFrame().getButtons()
							.addToNode(
									pageNode,
									MainInterface.thisIsIt.getRightFrame()
											.getPdfCalque());
					MainInterface.thisIsIt.getLeftFrame().getTree().mainTree
							.updateUI();
					MainInterface.thisIsIt.getLeftFrame().getTree().mainTree
							.treeDidChange();
					MainInterface.thisIsIt.getRightFrame().getPdfSelections()
							.repaint();
				} else {
					resetSelection();
					repaint();	
				}
			}

			public void mouseExited(MouseEvent e) {
				MainInterface.thisIsIt.getLeftFrame().getInformation()
						.getxMouse().setText("x = ");
				MainInterface.thisIsIt.getLeftFrame().getInformation()
						.getyMouse().setText("y = ");
			}
		});

		addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseMoved(MouseEvent e) {
				if (aGroupIsScalled()) {
					PageNode pageNode = MainInterface.thisIsIt.getLeftFrame()
							.getTree().currentNodePage();
					MCQReaderButtons buttons = MainInterface.thisIsIt
							.getLeftFrame().getButtons();
					GroupNode node;
					if (buttons.getTypeSelected() == 0)/* Radios */
						node = (GroupNode) pageNode.getRadioNode().getChildAt(
								buttons.getGroupeSelected() - 1);
					else
						/* Cases */
						node = (GroupNode) pageNode.getCaseNode().getChildAt(
								buttons.getGroupeSelected() - 1);
					int height = node.getHeight();
					int width = node.getWidth();
					selection.setBounds(e.getX() - width / 2, e.getY() - height
							/ 2, width, height);
					
					MainInterface.thisIsIt.getRightFrame().getPdfCalque()
							.repaint();
				}
				MainInterface.thisIsIt.getLeftFrame().getInformation()
						.getxMouse().setText("x = " + e.getX());
				MainInterface.thisIsIt.getLeftFrame().getInformation()
						.getyMouse().setText("y = " + e.getY());
			}

			public void mouseDragged(MouseEvent e) {
				if (!aGroupIsScalled()) {
					int xOrigin, yOrigin, width, height;
					if (e.getX() - xClick > 0) {
						xOrigin = xClick;
						width = e.getX() - xClick;
					} else {
						xOrigin = e.getX();
						width = xClick - e.getX();
					}
					if (e.getY() - yClick > 0) {
						yOrigin = yClick;
						height = e.getY() - yClick;
					} else {
						yOrigin = e.getY();
						height = yClick - e.getY();
					}

					// TODO : ajouter un epsilon pour la detection
					if (width != selection.width || height != selection.height)
						repaint();
					selection.setBounds(xOrigin, yOrigin, width, height);
					repaint();
				}
			}
		});
	}

	public Graphics myGraphics() {
		return this.getComponentGraphics(getGraphics());
	}

	protected void paintComponent(Graphics g) {
		g.drawRect(selection.x, selection.y, selection.width, selection.height);
	}

	public boolean isSelected() {
		int MinSegmentSelection = 5;
		return getSelection().width >= MinSegmentSelection
				&& getSelection().height >= MinSegmentSelection;
	}

	public Rectangle getSelectionClone() {
		return (Rectangle) getSelection().clone();
	}

	public String getSelectionString() {
		return "(" + getSelection().x + "," + getSelection().y + ","
				+ getSelection().height + "," + getSelection().width + ")";
	}

	static public boolean aGroupIsScalled() {
		PageNode pageNode = MainInterface.thisIsIt.getLeftFrame().getTree()
				.currentNodePage();
		MCQReaderButtons buttons = MainInterface.thisIsIt.getLeftFrame()
				.getButtons();
		GroupNode node;
		if (buttons.getTypeSelected() == 0 && buttons.getGroupeSelected() > 0) {/* Radios */
			node = (GroupNode) pageNode.getRadioNode().getChildAt(
					buttons.getGroupeSelected() - 1);
			return node.isScaled();
		} else if (buttons.getTypeSelected() == 1
				&& buttons.getGroupeSelected() > 0) {/* Cases */
			node = (GroupNode) pageNode.getCaseNode().getChildAt(
					buttons.getGroupeSelected() - 1);
			return node.isScaled();
		} else
			return false;
	}

	public void resetSelection() {
		selection.setBounds(0, 0, 0, 0);
	}
}
